package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.WatermeterPriceDao;
import cn.topevery.garden.entity.po.WatermeterPrice;
import cn.topevery.garden.entity.query.WatermeterPriceQuery;
import cn.topevery.garden.entity.vo.WatermeterPriceVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-02-16
 */
@Repository
public class WatermeterPriceDaoImpl extends BaseDaoImpl<WatermeterPrice> implements WatermeterPriceDao {
    @Override
    public <P extends DbCsp> List<WatermeterPrice> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<WatermeterPriceVO> listPage(WatermeterPriceQuery para) {
        String table = "t_watermeter_price as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_db_create_id";

        String columns = "price.c_id as id,\n" +
                "        price.c_watermeter_price_id as watermeterPriceId,\n" +
                "            price.c_map_id as mapId,\n" +
                "\t\t\t map.c_name as area,\n" +
                "\t\t\t price.c_watermeter_price_name as watermeterPriceName,\n" +
                "\t\t\t price.c_price as price,\n" +
                "\t\t\t usr.c_user_name as creator,\n" +
                "\t\t\t DATE_FORMAT( price.c_db_create_date, '%Y-%m-%d %H:%i:%s' ) AS dbCreateDate,\n" +
                "\t\t\t price.c_in_use as inUse";

        int pageIndex = para.pageIndex - 1;
        int pageSize = para.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_id desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "price.c_in_use", ScriptConditionEnum.Eq, 1)
                .where(!StringUtils.isEmpty(para.getMapId()), "price.c_map_id", ScriptConditionEnum.Eq, para.getMapId())
                .where(!StringUtils.isEmpty(para.getName()), "price.c_watermeter_price_name", ScriptConditionEnum.Like, para.getName())

        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, WatermeterPriceVO.class);
    }

    public DataPager<WatermeterPriceVO> getHisList(WatermeterPriceQuery para) {
        String table = "t_watermeter_price as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_db_create_id";

        String columns = "price.c_id as id,\n" +
                "           map.c_name as area,\n" +
                "           price.c_watermeter_price_name as name,\n" +
                "           price.c_price as unitprice,\n" +
                "           usr.c_user_name as creator,\n" +
                "           DATE_FORMAT(price.c_db_create_date, '%Y-%m-%d %H:%i:%s') as create_time";

        int pageIndex = para.pageIndex - 1;
        int pageSize = para.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_db_create_date desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "price.c_watermeter_price_id", ScriptConditionEnum.Eq, para.getEntityId())
                .where(true, "price.c_map_id", ScriptConditionEnum.Eq, para.getMapId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, WatermeterPriceVO.class);
    }

    @Override
    public WatermeterPrice getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }
}
